$(function () {
    // 显示注册页面
    $('.to_register').click(function () {
        $('.login_form').hide();
        $('.register_form').show();
        get_auth_code()
    });
    // 注册表单提交
    $(".register_form").submit(function (e) {
        e.preventDefault();
        var telephone = $(" #telephone_r").val();
        var password1 = $(" #password1_r").val();
        var password2 = $(" #password2_r").val();
        var auth_code = $("#auth_code_r").val();  // 提取图片验证码
        if (!telephone) {
            alert("请填写正确手机号");
            return;
        }
        if (!password1) {
            alert("请输入密码");
            return;
        }
        if (password1 != password2) {
            alert("两次密码不一致");
            return;
        }

        var params = {
            "telephone": telephone,
            "password": password1,
            "auth_code": auth_code
        };

        $.ajax({
            url: "/passport/register",
            method: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.err == "0") {
                    // 刷新界面
                    location.reload();
                } else {
                    alert(resp.errmsg)
                    get_auth_code()
                }
            }
        })
    });
    // 显示登录页面
    $('.to_login').click(function () {
        $('.register_form').hide();
        $('.login_form').show();
        generateImageCode()
    });
    // 登录表单提交
    $(".login_form").submit(function (e) {
        // alert(1111111)
        e.preventDefault();
        var telephone = $(" #telephone").val();
        var password = $(" #password").val();

        if (!telephone) {
            alert("请输入用户名")
        } else if (!password) {
            alert("请输入密码")
        }

        var params = {
            "telephone": telephone,
            "password": password,
        };

        $.ajax({
            url: "/passport/login",
            method: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.err == "0") {
                    // alert(resp.errmsg)
                    window.location.href = "/index";
                    // 刷新当前界面
                    // window.history.back(-1);
                    location.reload();
                } else {
                    // $("#login-password-err").html(resp.errmsg);
                    // $("#login-password-err").show()
                    alert(resp.errmsg)
                }
            }
        })
    });
});

// 生成验证码
function get_auth_code() {
    // 1. 生成一个编号
    imageCodeId = generateUUID();
    // 2. 拼接验证码地址
    var imageCodeUrl = "/passport/image_code?code_id=" + imageCodeId;
    // 3. 设置页面中图片验证码img标签的src属性
    $(".get_pic_code").attr("src", imageCodeUrl)
}

function generateUUID() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
        d += performance.now(); //use high-precision timer if available
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}